package com.lcm.jnode.model;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.google.common.collect.Lists;
import com.jfinal.kit.StringKit;
import com.jfinal.plugin.activerecord.Page;
import com.lcm.jnode.config.Constants;

/**
 * blog 实体
 * @author L.cm
 * @date 2013-5-7 上午9:30:18
 */
public class Blog extends DbModel<Blog> {

	private static final long serialVersionUID = -2208881735662609833L;
	public static final Blog dao = new Blog();
	
	public static final String ID = "id";					// id
	public static final String TITLE = "title";				// 标题
	public static final String CONTENT = "content";			// 内容
	public static final String USER_ID = "user_id";			// user id
	public static final String VIEW_COUNT = "view_count";	// 查看数
	public static final String BLOG_TYPE = "blog_type";		// 默认：0 （原创） 收藏：1   手游新闻：2
	public static final String[] TYPE_NAME = new String[]{"原创", "收藏"};
	public static final String[] TYPE_URL  = new String[]{"/blogs", "/favorites"};
	public static final String SHARE_URL = "share_url";		// 分享 url
	public static final String DEL_STATUS = "del_status";	// 是否删除 默认：0  删除：1
	public static final int DEL_N = 0;	// 未删
	public static final int DEL_Y = 1;	// 已删
	public static final String CREATE_TIME = "create_time"; // 添加时间
	public static final String UPDATE_TIME = "update_time"; // 更新时间
	
	public static final int BLOG_SIZE = 497; 				// 首页显示博文长度
	/**
	 * 分页查找
	 * @param @param pageNum
	 * @param @param pageSize
	 * @param @return    设定文件
	 * @return Page<Blog>    返回类型
	 * @throws
	 */
    public Page<Blog> page(Integer pageNum, int pageSize, Map<String, Object> result) {
        final List<Object> parameters = Lists.newArrayList();
        String select = "SELECT b.*, u.nick_name, u.url";
        StringBuilder sqlOutSelect = new StringBuilder("FROM blog AS b ,user_info AS u WHERE b.user_id = u.id AND b.del_status = 0 AND u.del_status = 0");
        if(StringKit.notNull(result.get(Blog.BLOG_TYPE))) {
            sqlOutSelect.append(" AND b.blog_type = ? ");
            parameters.add(result.get(Blog.BLOG_TYPE));
        }
        if(StringKit.notNull(result.get("s"))){
            sqlOutSelect.append(" AND b.title like ? OR b.content like ? ");
            parameters.add("%" + result.get("s") + "%");
            parameters.add("%" + result.get("s") + "%");
        }
        sqlOutSelect.append(" ORDER BY b.id DESC");
        return dao.paginateByCache(Constants.CACHE_TIME_MINI, pageNum, pageSize, select, sqlOutSelect.toString(), parameters.toArray());
    }

    /**
     * 根据id查找blog相关信息
     * @param @param paraToInt
     * @param @return    设定文件
     * @return Blog    返回类型
     * @throws
     */
    public Blog findFallById(Integer blogId) {
        String sql = "SELECT b.*, u.nick_name, u.url, u.head_photo, u.signature FROM blog AS b ,user_info AS u WHERE b.id = ? AND b.user_id = u.id AND b.del_status = 0 AND u.del_status = 0 limit 1";
        return dao.findFirstByCache(Constants.CACHE_TIME_MINI, sql, blogId);
    }

    /**
     * 最近的5条
     * @param @return    设定文件
     * @return List<Blog>    返回类型
     * @throws
     */
    public List<Blog> findLateList() {
        String sql = "SELECT b.id, b.title, b.update_time FROM blog AS b WHERE b.del_status = 0 ORDER BY b.id DESC LIMIT 5";
        return dao.findByCache(Constants.CACHE_TIME_MINI, sql);
    }

    /**
     * 点击最多的5条
     * @param @return    设定文件
     * @return List<Blog>    返回类型
     * @throws
     */
    public List<Blog> findHotList() {
    	String sql = "SELECT b.id, b.title, b.update_time, b.view_count FROM blog AS b WHERE b.del_status = 0 ORDER BY b.view_count DESC LIMIT 5";
        return dao.findByCache(Constants.CACHE_TIME_MINI, sql);
	}
    
    /**
     * 返回所有的未删除的博文
     * @param @return    设定文件
     * @return List<Blog>    返回类型
     * @throws
     */
    public List<Blog> find4github() {
        String sql = "SELECT b.* FROM blog AS b WHERE b.del_status = 0 ORDER BY b.id DESC";
        return dao.findByCache(Constants.CACHE_TIME_MINI, sql);
    }

    /**
     * rss List
     * @param limit
     * @return
     */
    public List<Blog> rssList(int limit) {
    	String sql = "SELECT b.*, u.nick_name FROM blog AS b, user_info AS u WHERE  b.user_id = u.id AND b.del_status = 0 AND u.del_status = 0 ORDER BY b.id DESC limit ?";
    	return dao.findByCache(Constants.CACHE_TIME_MINI, sql, limit);
    }

    /**
     * 保存blog实体
     * @param @param blog    设定文件
     * @return void    返回类型
     * @throws
     */
    public boolean save(Blog blog) {
        return blog.set("create_time", new Date()).set("update_time", new Date()).save();
    }

    /**
     * 更新blog实体
     * @param @param blog
     * @param @return    设定文件
     * @return boolean    返回类型
     * @throws
     */
	public boolean update(Blog blog) {
		return blog.set("update_time", new Date()).update();
	}
}
